package org.biubiu0002.baiblog.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.biubiu0002.baiblog.common.exception.BaiBlogException;
import org.biubiu0002.baiblog.common.exception.BizCodeEnumException;
import org.biubiu0002.baiblog.core.dao.GroupInfoDao;
import org.biubiu0002.baiblog.core.model.dto.TokenUserDto;
import org.biubiu0002.baiblog.core.model.entity.GroupInfoEntity;
import org.biubiu0002.baiblog.core.model.entity.GroupUserEntity;
import org.biubiu0002.baiblog.core.model.entity.MessageEntity;
import org.biubiu0002.baiblog.core.model.vo.UserInfoVo;
import org.biubiu0002.baiblog.core.model.vo.res.GroupInfoResVo;
import org.biubiu0002.baiblog.core.service.ConversationService;
import org.biubiu0002.baiblog.core.service.GroupInfoService;
import org.biubiu0002.baiblog.core.service.GroupUserService;
import org.biubiu0002.baiblog.core.service.MessageService;
import org.biubiu0002.baiblog.core.utils.IdGenerator;
import org.biubiu0002.baiblog.core.utils.SecurityUtils;
import org.biubiu0002.baiblog.im.constants.MessageConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: biubiu0002
 * @Date: 2022/12/8 14:09
 **/
@Service
public class GroupInfoServiceImpl extends ServiceImpl<GroupInfoDao, GroupInfoEntity> implements GroupInfoService {

    @Resource
    private GroupUserService groupUserService;

    @Resource
    private ConversationService conversationService;

    @Resource
    private MessageService messageService;

    @Override
    public GroupInfoResVo getGroupInfo(String uid) {
        GroupInfoResVo groupInfoResVo = new GroupInfoResVo();
        GroupInfoEntity groupInfoFromDB = this.getGroupInfoFromDB(uid);
        List<UserInfoVo> groupUserInfo = groupUserService.getGroupUserInfo(uid);
        groupInfoResVo.setGroupInfo(groupInfoFromDB);
        groupInfoResVo.setUserInfoVos(groupUserInfo);
        return groupInfoResVo;
    }

    @Override
    public GroupInfoEntity getGroupInfoByUid(String uid) {
        return this.getGroupInfoFromDB(uid);
    }

    @Override
    @Transactional(rollbackFor = BaiBlogException.class)
    public GroupInfoEntity saveGroup() {
        TokenUserDto user = SecurityUtils.getUser();
        if(user==null){
            throw new BaiBlogException(BizCodeEnumException.NOT_LOGIN_EXCEPTION);
        }
        String userUid = user.getUserUid();
        GroupInfoEntity groupInfoEntity = new GroupInfoEntity();
        Date date = new Date();
        String avatar = "https://img1.baidu.com/it/u=46425633,3623027485&fm=253";
        String groupUid = IdGenerator.generateUid();
        groupInfoEntity.setId(IdGenerator.generateId());
        groupInfoEntity.setResourceId(groupUid);
        groupInfoEntity.setName("群聊");
        groupInfoEntity.setOwnerId(userUid);
        groupInfoEntity.setCreatorId(userUid);
        groupInfoEntity.setCreateTime(date);
        //群聊头像
        groupInfoEntity.setAvatar(avatar);
        this.save(groupInfoEntity);
        GroupUserEntity groupUserEntity = new GroupUserEntity();
        groupUserEntity.setGroupId(groupUid);
        groupUserEntity.setUserId(userUid);
        groupUserService.save(groupUserEntity);
        //创建群聊 消息落库
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.setMsgId(IdWorker.getId());
        messageEntity.setMsgType(MessageConstants.MessageCommandType.GROUP_CREATED_MESSAGE);
        messageEntity.setFromUid(userUid);
        messageEntity.setToUid(groupUid);
        //不一致问题
        messageEntity.setMsgContent(String.format("%s创建了群聊，可以开始聊天了",user.getNickname()));
        messageEntity.setSendTime(date);
        messageService.save(messageEntity);
        //创建群主->群聊的会话列表
        conversationService.saveGroupConversation(userUid,groupInfoEntity);
        return groupInfoEntity;
    }



    public GroupInfoEntity getGroupInfoFromDB(String groupUid){
        return this.getOne(new QueryWrapper<GroupInfoEntity>().eq("resource_id", groupUid).eq("deleted", 0));
    }
}
